﻿/* 
┏━━━━━━━━━━━＼／━━━━━━━━━━━┓      
┃┏━━━━━━━━━━━━━━━━━━━━━━┓┃
     ------------------------------------------    
       Author           : TengSea   
       Created          : Mouth-Day-Year                              
       Last Modified By : TengSea                                 
       Last Modified On : Mouth-Day-Year                                                               
       Description      : 
     __________________________________________
     Copyright (c) TengSea. All rights reserved.
 ┃┗━━━━━━━━━━━━━━━━━━━━━━┛┃                            
 ┗━━━━━━━━━∪━━━━∪━━━━━━━━━┛
 */

using Hhmocon.Mes.DataBase;
using Hhmocon.Mes.DataBase.SqlServer;
using System;
using System.Collections.Generic;
using System.Linq;
using Hhmocon.Mes.Util;
using Hhmocon.Mes.Util.String;
using Dapper;
using System.Data;

namespace Hhmocon.Mes.Repository.Repository
{
    /// <summary>
    /// 仓储层所有能用泛型简化的代码
    /// 所有对数据的增删改查都可以由这个类做掉
    /// </summary>
    public class PikachuRepository
    {
        private SqlAssemble _sqlAssemble;
        public PikachuRepository(SqlAssemble sqlAssemble)
        { 
            _sqlAssemble = sqlAssemble;
        }

        /// <summary>
        /// 根据id获取单个数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T Get_byId<T>(string id)
        {
            using (var conn = SqlServerDbHelper.GetConn())
            {
                T data = conn.GetById<T>(id);
                return data;
            }
        }

        /// <summary>
        /// 根据Ids获取全部数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<T> Get_byIds<T>(string[] ids)
        {
            using (var conn = SqlServerDbHelper.GetConn())
            {
                List<T> data = conn.GetByIds<T>(ids).ToList();
                return data;
            }
        }


        /// <summary>
        /// 根据code获取数据
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public T Get_byCode<T>(string code)
        {
            using (var conn = SqlServerDbHelper.GetConn())
            {
                T data = conn.GetByCode<T>(code);
                return data;
            }
        }

        /// <summary>
        /// 根据Name获取
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <returns></returns>
        public T Get_byName<T>(string name ,IDbTransaction tran = null, int? commandTimeout = null)
        {
            using (var conn = SqlServerDbHelper.GetConn())
            {

                string chartName = _sqlAssemble.getClassName<T>();
                string sql = string.Format("select * from {0} where material_type_name = @name",chartName);

                T data = conn.QueryFirstOrDefault<T>(sql, new { name = name },tran,commandTimeout);
                return data;
            }
        }

        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <param name="wherestr"></param>
        /// <param name="sortstr"></param>
        /// <returns></returns>
        public List<T> GetAll2<T>(string wherestr, string sortstr)
        {
            using (var conn = SqlServerDbHelper.GetConn())
            {
                List<T> listData = null;
                listData = conn.GetByWhere<T>(where: wherestr, orderBy: sortstr).ToList();
                return listData;
            }
        }

        /// <summary>
        /// 获取所有delete_mark=0的数据
        /// </summary>
        /// <param name="wherestr"></param>
        /// <param name="sortstr"></param>
        /// <returns></returns>
        public List<T> GetAll<T>(string wherestr, string sortstr)
        {
            using (var conn = SqlServerDbHelper.GetConn())
            {
                List<T> listData = null;

                listData = conn.GetByWhere<T>(where: wherestr, orderBy: sortstr).ToList();
                return listData;
            }
        }

        /// <summary>
        /// 获取当前条件下的数据记录数
        /// </summary>
        /// <param name="wherestr"></param>
        /// <returns></returns>
        public long GetCount<T>(string wherestr)
        {
            using (var conn = SqlServerDbHelper.GetConn())
            {
                return conn.GetTotal<T>(wherestr);
            }
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="wherestr"></param>
        /// <param name="sortstr"></param>
        /// <returns></returns>
        public List<T> GetList<T>(int page, int rows, string wherestr, string sortstr)
        {
            using (var conn = SqlServerDbHelper.GetConn())
            {
                List<T> listdata = null;
                listdata = conn.GetByPageIndex<T>(page, rows, where: wherestr, orderBy: sortstr).ToList();
                return listdata;
            }
        }

        /// <summary>
        /// 添加用户数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool Insert<T>(T obj)
        {
            using (var conn = SqlServerDbHelper.GetConn())
            {
                int iret = conn.Insert(obj);
                return iret > 0;
            }
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool Update<T>(T data)
        {
            using (var conn = SqlServerDbHelper.GetConn())
            {
                int iret = conn.Update(data);
                return iret > 0;
            }
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete<T>(string[] id)
        {
            using (var conn = SqlServerDbHelper.GetConn())
            {
                int iret = conn.DeleteByIds<T>(id);
                return iret > 0;
            }
        }

        /// <summary>
        /// 根据id数组 假删除 相应的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool Delete_Mask<T>(string[] ids)
        {
            using (var conn = SqlServerDbHelper.GetConn())
            {
                List<T> listdata = null;
                listdata = conn.GetByIds<T>(ids).ToList();
                int iret = 0;
                foreach (dynamic temp in listdata)
                {
                    temp.delete_mark = 1;

                    //这里用了强制转换，可能有风险，后续改进
                    conn.Update((T)temp);

                    iret++;
                }

                return iret > 0;
            }
        }

        /// <summary>
        /// 通过sql和sortstr查询内容，返回列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="sortstr"></param>
        /// <returns></returns>
        public List<T> Get_bySql<T>(string sql, string sortstr)
        {
            using (var conn = SqlServerDbHelper.GetConn())
            {
                List<T> data = conn.GetByWhere<T>(where: sql, orderBy: sortstr).ToList();

                return data;
            }
        }

        public List<T> Get_bySql<T>(string sql)
        {
            using (var conn = SqlServerDbHelper.GetConn())
            {
                List<T> data = conn.GetByWhere<T>(where: sql).ToList();

                return data;
            }
        }

    }

}
